package com.yangrong.thread;

public class TraditionalThreadSynchronized {

	public static void main(String[] args) {
		new TraditionalThreadSynchronized().init();
	}
	
	private void init() {
		final OutPuter output = new OutPuter();
		new Thread(new Runnable() {
			@Override
			public void run() {
				while(true) {
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					output.output("Thread1");
				}
			}
		}).start();
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				while(true) {
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					output.output("Thread2");
				}
			}
		}).start();
	}
	
	class OutPuter {
		public void output(String name) {
			int len = name.length();
			synchronized (this) {
				for(int i = 0;i < len; i++) {
					System.out.print(name.charAt(i));
				}
				System.out.println();
			}
		}
		
//		public synchronized void output(String name) {
//			int len = name.length();
//			for(int i = 0;i < len; i++) {
//				System.out.print(name.charAt(i));
//			}
//			System.out.println();
//		}
	}
}
